//
//  Counter.m
//  InteractiveNotification
//
//  Created by GuochengLiu on 2017/1/17.
//  Copyright © 2017年 sumavision. All rights reserved.
//

#import "Counter.h"

@interface Counter ()

@property (nonatomic, assign) NSInteger internalValue;

@end

@implementation Counter

+ (instancetype)sharedInstance
{
    static dispatch_once_t once;
    static id sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init
{
    if (self = [super init]) {
        self.internalValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"internalValue"];
    }
    return self;
}


+ (NSInteger)currentValue
{
    return [Counter sharedInstance].internalValue;
}

+ (NSInteger)increase
{
    [Counter sharedInstance].internalValue = [Counter sharedInstance].internalValue + 1;
    [[NSUserDefaults standardUserDefaults] setInteger:[Counter sharedInstance].internalValue forKey:@"internalValue"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CounterDidChange" object:nil];
    
    return [Counter sharedInstance].internalValue;
}

+ (NSInteger)decrease
{
    [Counter sharedInstance].internalValue = [Counter sharedInstance].internalValue - 1;
    [[NSUserDefaults standardUserDefaults] setInteger:[Counter sharedInstance].internalValue forKey:@"internalValue"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CounterDidChange" object:nil];

    return [Counter sharedInstance].internalValue;
}

+ (NSInteger)reset
{
    [Counter sharedInstance].internalValue = 0;
    [[NSUserDefaults standardUserDefaults] setInteger:[Counter sharedInstance].internalValue forKey:@"internalValue"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CounterDidChange" object:nil];

    return [Counter sharedInstance].internalValue;
}


@end
